package com.prs.crm.dao.ca.work.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.ProcureStoreDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.ProcureStore;
import com.prs.crm.domain.ca.work.PurchaseApply;

public class ProcureStoreDaoImpl extends BaseDaoImpl<ProcureStore> implements
		ProcureStoreDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProcureStore> getAllProcureStore() {
		return (List<ProcureStore>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ProcureStore.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public ProcureStore getProcureStore(final String psBillNumber) {
		return (ProcureStore) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria critera = session.createCriteria(ProcureStore.class);
				critera.add(Restrictions.eq("psBillNumber", psBillNumber));
				List<ProcureStore> procure = critera.list();
				if (procure.size() == 0) {
					return null;
				} else {
					return procure.get(0);
				}
			}
		});
	}

	@Override
	public void saveOrProcureStore(ProcureStore procureStore) {
		this.getHibernateTemplate().saveOrUpdate(procureStore);
	}

}
